package com.ruoyi.manager.task;

import com.ruoyi.manager.domain.Place;
import com.ruoyi.manager.domain.TReservation;
import com.ruoyi.manager.mapper.PlaceMapper;
import com.ruoyi.manager.mapper.TReservationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class PlaceTask {


    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private TReservationMapper tReservationMapper;


    /**
     * 定时检查场馆当前占用状态
     */
    @Scheduled(cron = "0 * * * * ?")
    @Transactional
    public void checkPlaceStatus(){
        System.out.println("定时检查场馆状态");

        //一次性查询所有场馆
        List<Place> places = placeMapper.selectAllPlaceList();

        //查询当前时间段的所有预约信息
        //当前时间段的前六小时 和 后六小时
        List<TReservation> reservations = tReservationMapper.selectByCurrentTime(LocalDateTime.now().minusHours(6) , LocalDateTime.now().plusHours(6));

        //匹配对应信息
        // 构建预约信息的Map，以场馆ID为key，提高查找效率
        Map<Integer, List<TReservation>> reservationMap = reservations.stream()
                .collect(Collectors.groupingBy(TReservation::getCourtId));

        // 更新场馆状态
        for (Place place : places) {
            List<TReservation> placeReservations = reservationMap.get(place.getId());
            if (placeReservations == null) {
                //该时间段没有预约信息 状态改为0 存入数据库
                place.setStatus(0);
                placeMapper.updatePlace(place);
                continue;
            }
            // 根据预约情况更新场馆状态
            updatePlaceStatus(place, placeReservations);
        }

    }


    /**
     * 修改预约状态 超过预约时长半小时的预约自动结束
     */
    @Scheduled(cron = "0 * * * * *")
    public void updateReservationStatus(){
        System.out.println("定时修改预约状态");
        //修改预约状态 超过预约时长半小时的预约自动结束 状态为0 1 变为 2
        //1.查出预约记录
        List<TReservation> reservations = tReservationMapper.selectByCurrentTimeAndStatus(LocalDateTime.now().minusHours(24), LocalDateTime.now());

        //遍历预约记录 找出 开始时间+预约时长 < 当前时间 + 10 分钟的预约记录 状态改成2已完成
        for (TReservation reservation : reservations) {
            if (reservation.getStartTime().plusMinutes(reservation.getDuration()).isBefore(LocalDateTime.now().plusMinutes(10))){
                reservation.setStatus(2);
                tReservationMapper.updateTReservation(reservation);
            }
        }
    }

    private void updatePlaceStatus(Place place, List<TReservation> placeReservations) {
        //如果当前时间在预约的时间段里 那么状态改为1
        for (TReservation reservation : placeReservations) {
            LocalDateTime startTime = reservation.getStartTime();
            LocalDateTime endTime = startTime.plusMinutes(reservation.getDuration());
            if (LocalDateTime.now().isAfter(startTime) && LocalDateTime.now().isBefore(endTime)){
                place.setStatus(1);
                placeMapper.updatePlace(place);
                return;
            }
        }
        //遍历结束之后 状态改为0
        place.setStatus(0);
        placeMapper.updatePlace(place);
    }

}
